<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgpu - compute geometry</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link type="text/css" rel="stylesheet" href="main.css">
	</head>
	<body>

		<div id="info">
			<a href="https://threejs.org" target="_blank" rel="noopener">three.js webgpu</a> - compute geometry
		</div>

		<script type="importmap">
			{
				"imports": {
					"three": "../build/three.webgpu.js",
					"three/tsl": "../build/three.webgpu.js",
					"three/addons/": "./jsm/"
				}
			}
		</script>

		<script type="module">

			import * as THREE from 'three';
			import { vec3, cos, sin, mat3, storage, Fn, instanceIndex, timerLocal } from 'three/tsl';

			import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';

			import { OrbitControls } from 'three/addons/controls/OrbitControls.js';

			let camera, scene, renderer;
			let computeUpdate;

			init();

			function init() {

				camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
				camera.position.set( 0, 0, 1 );

				scene = new THREE.Scene();
				scene.background = new THREE.Color( 0x333333 );

				new GLTFLoader().load( 'models/gltf/LeePerrySmith/LeePerrySmith.glb', function ( gltf ) {

					const mesh = gltf.scene.children[ 0 ];
					mesh.scale.setScalar( .1 );
					mesh.material = new THREE.MeshNormalMaterial();
					scene.add( mesh );

					//

					const positionBaseAttribute = mesh.geometry.attributes.position;
					const normalBaseAttribute = mesh.geometry.attributes.normal;

					// replace geometry attributes for storage buffer attributes

					const positionStorageBufferAttribute = new THREE.StorageBufferAttribute( positionBaseAttribute.count, 4 );
					const normalStorageBufferAttribute = new THREE.StorageBufferAttribute( normalBaseAttribute.count, 4 );

					mesh.geometry.setAttribute( 'position', positionStorageBufferAttribute );
					mesh.geometry.setAttribute( 'normal', normalStorageBufferAttribute );

					// compute shader

					const computeFn = Fn( () => {

						const positionAttribute = storage( positionBaseAttribute, 'vec3', positionBaseAttribute.count ).toReadOnly();
						const normalAttribute = storage( normalBaseAttribute, 'vec3', normalBaseAttribute.count ).toReadOnly();

						const positionStorageAttribute = storage( positionStorageBufferAttribute, 'vec4', positionStorageBufferAttribute.count );
						const normalStorageAttribute = storage( normalStorageBufferAttribute, 'vec4', normalStorageBufferAttribute.count );

						const time = timerLocal( 1 );
						const scale = 0.3;

						//

						const position = vec3( positionAttribute.element( instanceIndex ) );
						const normal = vec3( normalAttribute.element( instanceIndex ) );

						const theta = sin( time.add( position.y ) ).mul( scale );

						const c = cos( theta );
						const s = sin( theta );

						const m = mat3(
							c, 0, s,
							0, 1, 0,
							s.negate(), 0, c
						);

						const transformed = position.mul( m );
						const transformedNormal = normal.mul( m );

						positionStorageAttribute.element( instanceIndex ).assign( transformed );
						normalStorageAttribute.element( instanceIndex ).assign( transformedNormal );

					} );

					computeUpdate = computeFn().compute( positionBaseAttribute.count );

				} );

				// renderer

				renderer = new THREE.WebGPURenderer( { antialias: true } );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.setAnimationLoop( animate );
				document.body.appendChild( renderer.domElement );

				const controls = new OrbitControls( camera, renderer.domElement );
				controls.minDistance = .7;
				controls.maxDistance = 2;

				window.addEventListener( 'resize', onWindowResize );

			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			async function animate() {

				if ( computeUpdate ) await renderer.computeAsync( computeUpdate );

				renderer.render( scene, camera );

			}

		</script>
	</body>
</html>
